var MongoClient = require('mongodb').MongoClient,
	mongo = require('mongodb'),
	format = require('util').format;
var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';
var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT'] : 27017;
console.log(">> Connecting to " + host + ":" + port);

function Post(name, title, post, abstracts, keywords, category, original, visible, allowcomment, top){
	this.name = name;
	this.title = title;
	this.post = post;
	this.abstracts = abstracts;
	this.keywords = keywords;
	this.category = category;
	this.original = original;
	this.visible = visible;
	this.allowcomment = allowcomment;
	this.top = top;
}
module.exports = Post;

Post.prototype.save = function(callback){
	var date = new Date();
	var time = {
		date: date,
    	year : date.getFullYear(),
    	month : date.getFullYear() + "-" + (date.getMonth() + 1),
    	day : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
    	minute : date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " + 
    	date.getHours() + ":" + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) 
	};
	var post = {
		name: this.name,
		time: time,
		title: this.title,
		post: this.post,
		abstracts: this.abstracts,
		keywords: this.keywords,
		category: this.category,
		original: this.original,
		visible: this.visible,
		allowcomment: this.allowcomment,
		top: this.top
	};
	MongoClient.connect(format("mongodb://%s:%s/blog", host, port), function(err, db) {
		if(err){
			return callback(err);
		}
		db.collection('posts',function (err, collection){
			if(err){
				db.close();
				return callback(err);
			}
			collection.insert(post,{
				safe: true
			}, function (err){
				db.close();
				if(err){
					return callback(err);
				}
				callback(null);
			});
		});
	});
};
Post.getAll = function(name, callback){
	MongoClient.connect(format("mongodb://%s:%s/blog", host, port), function(err, db) {
		if(err){
			return callback(err);
		}
		db.collection('posts',function (err, collection){
			if(err){
				db.close();
				return callback(err);
			}
			var query = {};
			if(name){
				query.name = name;
			}
			collection.find(query).sort({
				time: -1
			}).toArray(function (err, docs){
				db.close();
				if(err){
					return callback(err);
				}
				callback(null,docs);
			});
		});
	});
};
Post.getOne = function(name, day, title, callback){
	MongoClient.connect(format("mongodb://%s:%s/blog", host, port), function(err, db) {
		if(err){
			return callback(err);
		}
		db.collection('posts',function (err, collection){
			if(err){
				db.close();
				return callback(err);
			}
			collection.findOne({
				"name":name,
				"time.day":day,
				"title":title
			},function(err,doc){
				db.close();
				if(err){
					return callback(err);
				}
				callback(null,doc);
			})
		})
	})
}

Post.update = function(name, day, title, post, callback){
	MongoClient.connect(format("mongodb://%s:%s/blog", host, port), function(err, db) {
		if(err){
			return callback(err);
		}
		db.collection('posts',function (err, collection){
			if(err){
				db.close();
				return callback(err);
			}
			collection.update({
				"name":name,
				"time.day":day,
				"title":title
			},{
				$set: {
					post: post.post,
					abstracts: post.abstracts,
					keywords: post.keywords,
					category: post.category==""?0:parseInt(post.category),
					original: post.original=="on"?true:false,
					visible: post.visible=="on"?true:false,
					allowcomment: post.allowcomment=="on"?true:false,
					top: post.top=="on"?true:false
				}
			},function(err){
				db.close();
				if(err){
					return callback(err);
				}
				callback(null);
			})
		})
	})
}